package org.dey.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.mapper.HealthMapper;
import org.dey.pojo.Health;
import org.dey.pojo.Library;
import org.dey.pojo.PageVo;
import org.dey.service.HealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HealthServiceimpl extends ServiceImpl<HealthMapper, Health> implements HealthService {
    @Autowired(required = false)
    HealthMapper healthMapper;

    @Override
    public PageVo<Health> queryHealth(Health health,int pageNum, int pageSize) {
        //组装条件
        QueryWrapper<Health> queryWrapper = new QueryWrapper<Health>();

        //根据关键字模糊查询
        if(StringUtils.isNotBlank(health.getKeywords())) {
            queryWrapper.like("keywords", health.getKeywords());
        }

        if(StringUtils.isNotBlank(health.getPlan())){
            queryWrapper.like("plan",health.getPlan());
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Health> healthList = this.list(queryWrapper);

        PageInfo<Health> pageInfo = new PageInfo<Health>(healthList);
        PageVo<Health> pageVo =new PageVo<Health>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());
        return pageVo;
    }

    @Override
    public int insertHealth(Health health) {
        int num = 0;

        QueryWrapper<Health> queryWrapper = new QueryWrapper<Health>();
        queryWrapper.eq("titles", health.getTitles());

        Health one = this.getOne(queryWrapper);

        if(one==null){
            boolean save = this.save(health);
            if(save){
                num=1;
            }
        }
        return num;
    }

    @Override
    public int deleteHealth(int id) {
        int num = 0;
        boolean b = this.removeById(id);
        if(b){
            num = 1;
        }
        return num;
    }

    @Override
    public Health queryHealthByid(int id) {
        return this.getById(id);
    }

    @Override
    public int updateHealth(Health health) {
        int num = 0;
        boolean b = this.updateById(health);
        if(b){
            num = 1;
        }
        return num;
    }
}
